/**
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.boding.power.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.boding.power.common.Constants;
import com.boding.power.model.SysDict;

/**
 * 字典项工具类
 * 
 * @author giegie
 * @date 2016年5月13日
 * @description
 * 
 */
public class DictUtils {
	public static String getDictLabel(String value, String type,
			String defaultValue) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
			for (SysDict dict : getDictList(type)) {
				if (type.equals(dict.getType())
						&& value.equals(dict.getValue())) {
					return dict.getLabel();
				}
			}
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type,
			String defaultLabel) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)) {
			for (SysDict dict : getDictList(type)) {
				if (type.equals(dict.getType())
						&& label.equals(dict.getLabel())) {
					return dict.getValue();
				}
			}
		}
		return defaultLabel;
	}

	public static List<SysDict> getDictList(String type) {
		String sql = "select * from sys_dict d where d.type = ? and d.del_flag = ? order by d.sort desc";
		List<SysDict> sysDicts = SysDict.dao.find(sql, type,Constants.FLAG_NORMAL);
		return sysDicts;
	}

}
